/****************************************************************************
*
*  ALi (Shanghai) Corporation, All Rights Reserved. 2003 Copyright (C)
*
*  File: win_ews.c
*
*  Description: 
*              
*  History:
*      Date        	Author             	Version     	Comment
*      ====        	======        	=======   	=======
*
****************************************************************************/
#include <sys_config.h>
#include <types.h>

#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>
#include <api/libchunk/chunk.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

//#include "win_com_menu_define.h"
#include "win_com.h"
#include "menus_root.h"
#include "win_com_popup.h"

#ifdef EWS_INDONESIA_DEMO

#include "ews_indonesia.h"

extern CONTAINER g_win_ews;

extern BITMAP ews_disaster_symbol_bitmap;
extern BITMAP ews_authority_logo_bitmap;

extern TEXT_FIELD ews_disaster_area_status_text;
extern TEXT_FIELD ews_disaster_location_text;

extern CONTAINER ews_disaster_type_con;
extern CONTAINER ews_disaster_time_con;   
extern CONTAINER ews_disaster_position_con;
extern CONTAINER ews_disaster_characteristic_con;
extern CONTAINER ews_disaster_suggestion_con;

extern TEXT_FIELD ews_disaster_type_text;
extern TEXT_FIELD ews_disaster_time_text;   
extern TEXT_FIELD ews_disaster_position_text;
extern TEXT_FIELD ews_disaster_characteristic_text;
extern TEXT_FIELD ews_disaster_suggestion_text;

extern TEXT_FIELD ews_disaster_type_msg;
extern TEXT_FIELD ews_disaster_time_msg;   
extern TEXT_FIELD ews_disaster_position_msg;
extern TEXT_FIELD ews_disaster_characteristic_msg;
extern TEXT_FIELD ews_disaster_suggestion_msg;


extern MULTI_TEXT ews_disaster_abstract_multi_text;

static VACTION win_ews_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_ews_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static PRESULT win_ews_message_proc(UINT32 param1, UINT32 param2);


#define WIN_SH_IDX	  WSTL_POP_WIN_01_HD//WSTL_POP_WIN_01_8BIT//
#define WIN_HL_IDX	  WSTL_POP_WIN_01_HD
#define WIN_SL_IDX	  WSTL_POP_WIN_01_HD
#define WIN_GRY_IDX   WSTL_POP_WIN_01_HD

#define TITLE_SH_IDX	WSTL_POP_TEXT_8BIT
#define SET_SH_IDX		WSTL_TEXT_30_8BIT

#define MTXT_MSG_SH_IDX		WSTL_TEXT_08_HD

#define BMP_SH_IDX   WSTL_MIXBACK_BLACK_IDX_HD


#define CON_SH_IDX   WSTL_NOSHOW_IDX//WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_02_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define	W_L     100
#define	W_T     60
#define	W_W     800
#define	W_H     540

#define BMP_SYM_L   (W_L + 40)
#define BMP_SYM_T   (W_T + 43)
#define BMP_SYM_W   114
#define BMP_SYM_H   114

#define BMP_LOGO_L  (W_W + W_L - 180)
#define BMP_LOGO_T  (W_T + 35)
#define BMP_LOGO_W  130
#define BMP_LOGO_H  140

#define TXT_BIG_L (BMP_SYM_L + BMP_SYM_W + 20)
#define TXT_BIG_T (W_T + 60)  
#define TXT_BIG_W (BMP_LOGO_L - TXT_BIG_L) 
#define TXT_BIG_H 40  

#define TXT_BIG_H_OFFSET 40

#define TXT_CON_L (W_L + 40) 
#define TXT_CON_T (W_T + 180)
#define TXT_CON_W (W_W - 40 - 20)
#define TXT_CON_H 38

#define TXT_CON_H_OFFSET 40

#define TXT_NAME_L TXT_CON_L
#define TXT_NAME_T (W_T + 180)
#define TXT_NAME_W 160
#define TXT_NAME_H 38

#define TXT_NAME_L_OFFSET 0
#define TXT_NMAE_H_OFFSET 40

#define TXT_MSG_L (TXT_NAME_L + TXT_NAME_W + 10)
#define TXT_MSG_T (W_T + 180)
#define TXT_MSG_W (TXT_CON_W + TXT_CON_L - TXT_MSG_L)
#define TXT_MSG_H 38

#define TXT_MSG_L_OFFSET (TXT_NAME_L_OFFSET + TXT_NAME_W)
#define TXT_MSG_H_OFFSET 40

#define MULTI_TXT_L (W_L + 40)
#define MULTI_TXT_T (TXT_NAME_T + TXT_MSG_H_OFFSET * 5 + 8)
#define MULTI_TXT_W (W_W - 40 - 20)
#define MULTI_TXT_H 130

TEXT_CONTENT ews_suggestion_text = {STRING_UNICODE, 0};

#define LDEF_MTXT(root,varMtxt,nextObj,l,t,w,h,cnt,content)	\
	DEF_MULTITEXT(varMtxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    	0,0,0,0,0, l,t,w,h, MTXT_MSG_SH_IDX,MTXT_MSG_SH_IDX,MTXT_MSG_SH_IDX,MTXT_MSG_SH_IDX,   \
	    NULL,NULL,  \
    	C_ALIGN_LEFT | C_ALIGN_TOP, cnt,  4,4,w-8,h-8,NULL,content)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    win_ews_keymap,win_ews_callback,  \
    nxtObj, focusID,0)


#define LDEF_BMP(root,varBmp,nxtObj,l,t,w,h,icon)		\
	DEF_BITMAP(varBmp,&root,nxtObj,C_ATTR_ACTIVE,0, \
	    0,0,0,0,0, l,t,w,h, BMP_SH_IDX,BMP_SH_IDX,BMP_SH_IDX,BMP_SH_IDX,   \
	    NULL,NULL,  \
	    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,icon)

#define LDEF_BIG(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,2, \
    0,0,0,0,0, l,t,w,h, TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TEXT(root,varTxt,nxtObj, ID, l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,0,0,0,0, l,t,w,h, SET_SH_IDX,SET_SH_IDX,SET_SH_IDX,SET_SH_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_CON(root, varCon,nxtObj,l,t,w,h,conobj)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_INACTIVE,0, \
    0,0,0,0,0, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
     NULL,NULL,  \
    conobj, 0,0)

#define LDEF_LIST_ITEM(root,varCon,nxtObj,varTxtName,varTxtMsg,l,t,w,h,TxtNameID,TxtNameStr,TxtMsgStr)	\
	LDEF_CON(&root, varCon,nxtObj,l,t,w,h,&varTxtName)	\
	LDEF_TEXT(varCon,varTxtName,&varTxtMsg ,0,l + TXT_NAME_L_OFFSET, t,TXT_NAME_W,h,TxtNameID,TxtNameStr)	\
	LDEF_TEXT(varCon,varTxtMsg, NULL,       0,l + TXT_MSG_L_OFFSET,  t,TXT_MSG_W,h,0,TxtMsgStr)	\

LDEF_WIN(g_win_ews, &ews_disaster_symbol_bitmap , W_L, W_T,W_W,W_H,1)

LDEF_BMP(g_win_ews, ews_disaster_symbol_bitmap, &ews_authority_logo_bitmap, BMP_SYM_L,BMP_SYM_T,BMP_SYM_W,BMP_SYM_H ,0)
LDEF_BMP(g_win_ews, ews_authority_logo_bitmap, &ews_disaster_area_status_text, BMP_LOGO_L,BMP_LOGO_T,BMP_LOGO_W,BMP_LOGO_H,0)

LDEF_BIG(g_win_ews, ews_disaster_area_status_text, &ews_disaster_location_text, TXT_BIG_L,TXT_BIG_T,TXT_BIG_W,TXT_BIG_H,0,display_strs[10])
LDEF_BIG(g_win_ews, ews_disaster_location_text, &ews_disaster_type_con, TXT_BIG_L,TXT_BIG_T + TXT_BIG_H_OFFSET ,TXT_BIG_W,TXT_BIG_H,0,display_strs[11])

#if 0

	LDEF_CON(&g_win_ews, ews_disaster_type_con,NULL,TXT_CON_L,TXT_CON_T,TXT_CON_W,TXT_CON_H,&ews_disaster_type_text)	
	LDEF_TEXT(ews_disaster_type_con,ews_disaster_type_text,&ews_disaster_type_msg ,1,TXT_CON_L + TXT_NAME_L_OFFSET, TXT_CON_T,TXT_NAME_W,TXT_CON_H,0,disaster_type_text)	
	LDEF_TEXT(ews_disaster_type_con,ews_disaster_type_msg, NULL,       2,TXT_CON_L + TXT_MSG_L_OFFSET,  TXT_CON_T,TXT_MSG_W,TXT_CON_H,0,disaster_type_msg)	


#else
LDEF_LIST_ITEM(g_win_ews, ews_disaster_type_con, &ews_disaster_time_con, ews_disaster_type_text,ews_disaster_type_msg,\
                TXT_CON_L,TXT_CON_T + TXT_CON_H_OFFSET * 0,TXT_CON_W,TXT_CON_H,0,display_strs[0],display_strs[1])
                
LDEF_LIST_ITEM(g_win_ews, ews_disaster_time_con, &ews_disaster_position_con, ews_disaster_time_text,ews_disaster_time_msg,\
                TXT_CON_L,TXT_CON_T + TXT_CON_H_OFFSET * 1,TXT_CON_W,TXT_CON_H,0,display_strs[2],display_strs[3])

LDEF_LIST_ITEM(g_win_ews, ews_disaster_position_con, &ews_disaster_characteristic_con, ews_disaster_position_text,ews_disaster_position_msg,\
                TXT_CON_L,TXT_CON_T + TXT_CON_H_OFFSET * 2,TXT_CON_W,TXT_CON_H,0,display_strs[4],display_strs[5])

LDEF_LIST_ITEM(g_win_ews, ews_disaster_characteristic_con, &ews_disaster_suggestion_con, ews_disaster_characteristic_text,ews_disaster_characteristic_msg,\
                TXT_CON_L,TXT_CON_T + TXT_CON_H_OFFSET * 3,TXT_CON_W,TXT_CON_H,0,display_strs[6],display_strs[7])

LDEF_LIST_ITEM(g_win_ews, ews_disaster_suggestion_con, &ews_disaster_abstract_multi_text, ews_disaster_suggestion_text,ews_disaster_suggestion_msg,\
                TXT_CON_L,TXT_CON_T + TXT_CON_H_OFFSET * 4,TXT_CON_W,TXT_CON_H,0,display_strs[8],display_strs[9])

LDEF_MTXT(g_win_ews, ews_disaster_abstract_multi_text, NULL,MULTI_TXT_L,MULTI_TXT_T,MULTI_TXT_W,MULTI_TXT_H,1,&ews_suggestion_text)
#endif



UINT16 disaster_imgae[] =
{
    0,
    IM_GEMPA_BUMI,
    IM_TSUNAMI,
    IM_LETUSAN_GUNUNG_BERAPI,
    IM_GERAKAN_TANAH,
    IM_BANJIR,
    IM_KEKERINGAN,
    IM_KEBAKARAN_HUTAN_DAN_LAHAN,
    IM_EROSI,
    IM_KEBAKARAN_GEDUNG_DAN_PEMUKIMAN,
    IM_GELOMBANG_EKSTRIM_DAN_ABRASI,
    IM_CUACA_EKSTRIM,
    IM_KEGAGALAN_TEKNOLOGI,
    IM_EPIDEMI_DAN_WABAH_PENYAKIT,
    IM_KONFLIK_SOSIAL,
    IM_CADANGAN,
};

UINT16 authority_imgae[] =
{
    0,
    IM_BMKG,
    IM_BNPB,
};

char *disaster_status[] = 
{
    NULL,
    "STATUS AWAS",    
    "STATUS SIAGA",
    "STATUS WASPADA",
};


static VACTION win_ews_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;
	
    switch(key)
    {
    case V_KEY_MENU:
    case V_KEY_EXIT:
        act = VACT_ENTER;
        break;
    default:
        act = VACT_PASS;
    }
    
	return act;
}

static void win_ews_init(void)
{
    UINT8 msg_str[300] = {0};
    ews_indonesia_t *ews_info;

    MEMSET(display_strs , 0x00, sizeof(display_strs[0]) * 13);
    
    ComAscStr2Uni("Bencana :" ,display_strs[0]);
    ComAscStr2Uni("Tenggal :" ,display_strs[2]);
    ComAscStr2Uni("Posisi:" ,display_strs[4]);
    ComAscStr2Uni("Karakter :" ,display_strs[6]);
    ComAscStr2Uni("Pesan :" ,display_strs[8]);

    ews_info = ap_ews_indonesia_get_event();

    if (ews_info != NULL)
    {
        ews_disaster_symbol_bitmap.wIconID = disaster_imgae[ews_info->disaster_code];
        ews_authority_logo_bitmap.wIconID = authority_imgae[ews_info->authority];

        ComAscStr2Uni(disaster_status[ews_info->warning_level],display_strs[10]);
        ComAscStr2Uni(ews_info->location_desc,display_strs[11]);

        ComAscStr2Uni(ews_info->disaster_desc,display_strs[1]);
        ComAscStr2Uni(ews_info->disaster_date,display_strs[3]);
        ComAscStr2Uni(ews_info->disaster_position,display_strs[5]);
        ComAscStr2Uni(ews_info->disaster_characteristic,display_strs[7]);
        ComAscStr2Uni("",display_strs[9]);

        
        sprintf(msg_str, "%s,%s\n%s", ews_info->disaster_desc, ews_info->message_content, disaster_status[ews_info->warning_level]);
        ComAscStr2Uni(msg_str,display_strs[12]);
        ews_suggestion_text.text.pString = display_strs[12];
    }
    return;
}

static PRESULT win_ews_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;	
    VACTION unact = VACT_PASS; 
    
	switch(event)
	{
	case EVN_PRE_OPEN:
        win_ews_init();
		break;
	case EVN_POST_OPEN:
		break;
	case EVN_PRE_CLOSE:
        *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	     
		break;
	case EVN_POST_CLOSE:
		break;
    case EVN_UNKNOWN_ACTION :
        unact = (VACTION)(param1>>16);
        if(unact == VACT_ENTER)
        {
            OSD_ClearObject((POBJECT_HEAD)&g_win_ews, C_UPDATE_ALL);
            menu_stack_pop();
        }
        break;
	case EVN_MSG_GOT:
		ret = win_ews_message_proc(param1,param2);
		break;
    default :
        break;
	}
	return ret;
}

static PRESULT win_ews_message_proc(UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;	

    return ret;
}
#endif
